package com.nanning.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nanning.common.utils.StringUtils;
import com.nanning.system.domain.TodayData;
import com.nanning.system.mapper.TodayDataMapper;
import com.nanning.system.service.ITodayDataService;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 今日数据概览 服务实现类
 * </p>
 *
 * @author zhurenwang
 * @since 2025-05-03
 */
@Service
public class TodayDataServiceImpl extends ServiceImpl<TodayDataMapper, TodayData> implements ITodayDataService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importData4(Sheet sheet4) {
        String type = "各站承认车情况";
        LambdaQueryWrapper<TodayData> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TodayData::getType, type);
        baseMapper.delete(wrapper);
        int rows = sheet4.getPhysicalNumberOfRows();
        for (int i = 0; i < rows + 1; i++) {
            if (i < 4) continue;
            TodayData todayData = new TodayData();
            todayData.setType(type);
            todayData.setTitle(sheet4.getRow(i).getCell(0).getStringCellValue());
            todayData.setValue(String.valueOf(sheet4.getRow(i).getCell(1).getNumericCellValue()));
            save(todayData);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importData5(Sheet sheet5) {
        String type = "各品类装车需求情况";
        LambdaQueryWrapper<TodayData> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TodayData::getType, type);
        baseMapper.delete(wrapper);
        int rows = sheet5.getPhysicalNumberOfRows();
        for (int i = 0; i < rows + 1; i++) {
            if (i < 4) continue;
            TodayData todayData = new TodayData();
            todayData.setType(type);
            todayData.setTitle(sheet5.getRow(i).getCell(0).getStringCellValue());
            todayData.setValue(String.valueOf(sheet5.getRow(i).getCell(1).getNumericCellValue()));
            save(todayData);
        }
    }

    @Override
    public void importData6(Sheet sheet6) {
        String type = "各站板材、石油承认车情况";
        LambdaQueryWrapper<TodayData> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TodayData::getType, type);
        baseMapper.delete(wrapper);
        int rows = sheet6.getPhysicalNumberOfRows();
        String lastStation = "";
        for (int i = 0; i < rows + 1; i++) {
            if (i < 4) continue;
            TodayData todayData = new TodayData();
            String station = sheet6.getRow(i).getCell(0).getStringCellValue();
            if (StringUtils.isEmpty(station)) {
                station = lastStation;
            }
            todayData.setType(type);
            todayData.setTitle(station + "-" + sheet6.getRow(i).getCell(1).getStringCellValue());
            todayData.setValue(String.valueOf(sheet6.getRow(i).getCell(2).getNumericCellValue()));
            save(todayData);
            lastStation = station;
        }
    }

    @Override
    public void importData7(Sheet sheet7) {
        String type = "各站集装箱承认车情况";
        LambdaQueryWrapper<TodayData> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TodayData::getType, type);
        baseMapper.delete(wrapper);
        int rows = sheet7.getPhysicalNumberOfRows();
        for (int i = 0; i < rows + 1; i++) {
            if (i < 4) continue;
            TodayData todayData = new TodayData();
            todayData.setType(type);
            todayData.setTitle(sheet7.getRow(i).getCell(0).getStringCellValue() + "-" + sheet7.getRow(i).getCell(1).getStringCellValue());
            todayData.setValue(String.valueOf(sheet7.getRow(i).getCell(2).getNumericCellValue()));
            save(todayData);
        }
    }
}
